package com.gpframework.module.controller.ctn;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.ctn.domain.GpCtnReachPort;
import com.gpframework.module.ctn.domain.req.GpCtnReachPortAdd;
import com.gpframework.module.ctn.domain.req.GpCtnReachPortReq;
import com.gpframework.module.ctn.domain.req.GpCtnReachPortUpdate;
import com.gpframework.module.ctn.domain.req.GpTelexReleaseAdd;
import com.gpframework.module.ctn.domain.vo.GpCtnReachPortExcel;
import com.gpframework.module.ctn.domain.vo.GpCtnReachPortVo;
import com.gpframework.module.ctn.service.IGpCtnReachPortService;
import com.gpframework.module.ctn.service.IGpTelexReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpCtnReachPortController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 装柜到港填写Controller
 */
@Validated
@Api(tags = "装柜到港填写")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/ctn/reachport" )
public class GpCtnReachPortController extends BaseController {

    private final IGpTelexReleaseService iGpTelexReleaseService;
    private final IGpCtnReachPortService iGpCtnReachPortService;


 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpCtnReachPort
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('ctn:reachport:list')")
    @GetMapping("/list")
    public TableDataInfo<GpCtnReachPortVo> list(GpCtnReachPortReq gpCtnReachPortReq)
    {
        startPage();
        QueryWrapper<GpCtnReachPort> lqw = new QueryWrapper<GpCtnReachPort>();
        gpCtnReachPortReq.generatorQuery(lqw,true);
        List<GpCtnReachPortVo> list = iGpCtnReachPortService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnReachPort
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('ctn:reachport:add')" )
    @Log(title = "添加装柜到港填写" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GpCtnReachPortAdd gpCtnReachPortAdd) {
        GpCtnReachPort gpCtnReachPort = Convert.convert(new TypeReference<GpCtnReachPort>() {}, gpCtnReachPortAdd);
        if (gpCtnReachPortAdd.getCtnReachPortId()==null){
            return toAjax(iGpCtnReachPortService.save(gpCtnReachPort)?1:0);
        }
        gpCtnReachPort.setId(gpCtnReachPortAdd.getCtnReachPortId());
        return toAjax(iGpCtnReachPortService.updateById(gpCtnReachPort) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnReachPort
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('ctn:reachport:edit')" )
    @Log(title = "修改装柜到港填写" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCtnReachPortUpdate gpCtnReachPortUpdate) {
        return toAjax(iGpCtnReachPortService.editCtnReachPort(gpCtnReachPortUpdate) ? 1 : 0);
    }


    /**
     * @Description 导出装柜到港填写模板
     * @Author gpframework
     * @Date 2023-05-24
     * @Param gpCustomer
     * @Return TableDataInfo
     */
    @ApiOperation("导出装柜到港填写模板")
    @PreAuthorize("@ss.hasPermi('ctn:reachport:importTemplate')")
    @Log(title = "导出装柜到港填写模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult exportTemplate() {
        ExcelUtil<GpCtnReachPortExcel> util = new ExcelUtil<>(GpCtnReachPortExcel.class);
        return util.exportExcel(GpCtnReachPortExcel.list(), "模板");
    }

    /**
     * @Description 导入装柜到港填写
     * @Author gpframework
     * @Date 2023-10-27
     * @Param (GpMember
     * @Return TableDataInfo
     */
    @Log(title = "导入装柜到港填写" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ctn:reachport:import')" )
    @PostMapping("/import")
    public AjaxResult entityImport(MultipartFile file, Long brandId, Long storeId) throws Exception {
        ExcelUtil<GpCtnReachPortExcel> util = new ExcelUtil<GpCtnReachPortExcel>(GpCtnReachPortExcel. class);
        List<GpCtnReachPortExcel> excelList = util.importExcel(file.getInputStream());

        return AjaxResult.builder(() -> {
            return this.iGpCtnReachPortService.gpCtnReachPortImport(excelList, brandId, storeId);
        });
    }

    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnReachPort
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('ctn:reachport:export')" )
    @Log(title = "导出装柜到港填写" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpCtnReachPortReq gpCtnReachPortReq) {
        QueryWrapper<GpCtnReachPort> lqw = new QueryWrapper<GpCtnReachPort>();
        gpCtnReachPortReq.generatorQuery(lqw,true);
        List<GpCtnReachPortVo> list = iGpCtnReachPortService.listVo(lqw);
        ExcelUtil<GpCtnReachPortVo> util = new ExcelUtil<GpCtnReachPortVo>(GpCtnReachPortVo. class);
        return util.exportExcel(list, "reachport" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnReachPort
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('ctn:reachport:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpCtnReachPortVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpCtnReachPortService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnReachPort
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('ctn:reachport:remove')" )
    @Log(title = "删除装柜到港填写" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCtnReachPortService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnReachPort
     * @Return TableDataInfo
     */
    @ApiOperation("新增货柜电放填写")
    @PreAuthorize("@ss.hasPermi('ctn:createTelexRelease:add')" )
    @Log(title = "新增货柜电放填写" , businessType = BusinessType.INSERT)
    @PostMapping("/createTelexRelease")
    public AjaxResult createTelexRelease(@Valid @RequestBody GpTelexReleaseAdd gpTelexReleaseAdd) {
        if (gpTelexReleaseAdd == null) {
            return AjaxResult.parameterError();
        }
        if (gpTelexReleaseAdd.getCtnOrderId()==null){
            return AjaxResult.error("ctnOrderId is null");
        }

        return toAjax(iGpTelexReleaseService.createTeleRelease(gpTelexReleaseAdd) ? 1 : 0);
    }
}
